<?php

namespace App\Repositories;

use App\Interfaces\ProductSkuService;

use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductAttributeValue;
use App\Models\ProductSku;
use App\Models\CategoryAttribute;
use App\Models\CategoryAttributeValue;

class ProductSkuRepository implements ProductSkuService
{

    /**
     * 关联产品和商品，并保存商品的属性以及对应的属性值的json字符串
     * @param  [int] $product_id        [产品id]
     * @param  [int] $goods_id          [商品id]
     * @param  [array] $attribute_group [商品的属性以及对应的属性值的数组]
     * @return mixed
     */
    public function addRelation($product_id, $goods_id, $attribute_groups){

        $product_sku['product_id'] = $product_id;
        $product_sku['goods_id'] = $goods_id;

        $product_sku = ProductSku::firstOrCreate($product_sku);

        if($product_sku){

            //$attribute_group = trim(json_encode($attribute_group));
            $num = 0;
            $attribute_group = '';
            foreach ($attribute_groups as $key => $value) {

                if($num==0){
                    $attribute_group = $value;
                }else{
                    $attribute_group .= ','.$value;
                }//end if()

                ++$num;

            }//end foreach
            $product_sku->attribute_group = $attribute_group;
            $product_sku->save();

            return $product_sku;
        }//end if()

        return false;
    }//end func


    /**
     * [根据商品id查找产品id以及该商品对应的属性值]
     * @param [type] $goods_id [description]
     */
    public function getByGoodsId($goods_id){

        $sku_info = [];
        //第一步：根据当前商品id获取所属产品信息
        $product_skus = ProductSku::firstOrNew(['goods_id' => $goods_id]);
        $sku_id = $product_skus->sku_id;
        $product_id = $product_skus->product_id;
        $attribute_group = $product_skus->attribute_group;
        $attribute_groups = explode(',', $attribute_group);
        $product_skus->attribute_group = $attribute_groups;

        //第二步：根据产品id获取所有的商品（sku）
        $product_sku = ProductSku::where('product_id', $product_id)->get();
        if ($product_sku) {
            $attribute_group_arr = [];
            foreach ($product_sku as $key => $value) {
                $attribute_group_arr[$key] = explode(',', $value->attribute_group);
            }//end foreach()
        }//end if()

        //第三步：根据产品id获取产品的属性及属性值
        $product_attribute_arr = [];
        $product_attributes = ProductAttribute::where('product_id', $product_id)->get();
        if($product_attributes){

            foreach ($product_attributes as $key => $value) {

                $product_attribute_id = $value->id;
                $category_attribute_id = $value->category_attribute_id;

                $product_attribute_arr[$key]['attribute_id'] = $category_attribute_id;
                $product_attribute_arr[$key]['attribute_name'] = CategoryAttribute::where('id', $category_attribute_id)->pluck('attribute_name');

                //获取产品的属性的属性值
                $product_attribute_values = ProductAttributeValue::where('product_attribute_id', $product_attribute_id)->get();
                foreach ($product_attribute_values as $keys => $values) {
                    $attribute_value_id = $values->attribute_value;
                    $product_attribute_arr[$key]['attribute_value_arr'][$keys]['attribute_value_id'] = $attribute_value_id;
                    $product_attribute_arr[$key]['attribute_value_arr'][$keys]['attribute_value_name'] = CategoryAttributeValue::where('id', $attribute_value_id)->pluck('attribute_value');

                    $attribute_value_status = 0;
                    foreach ($attribute_group_arr as $attribute_group_key => $attribute_group_value) {
                        if(in_array($attribute_value_id, $attribute_group_value)){
                            $attribute_value_status = 2;
                            if(in_array($attribute_value_id, $attribute_groups)){
                                $attribute_value_status = 1;
                                break;
                            }//end if()
                            break;
                        }//end if()
                    }//end foreach()
                    $product_attribute_arr[$key]['attribute_value_arr'][$keys]['attribute_value_status'] = $attribute_value_status;             //属性值的状态：0：不可被选中；1：当前选中；2：可以被选中
                }//end foreach()

            }//end foreach()

        }//end if()
        $sku_info['product_skus'] = $product_skus->toArray();
        $sku_info['product_attribute_arr'] = $product_attribute_arr;

        return $sku_info;
    }//end func getByGoodsId


    /**
     * 解除产品和商品的绑定
     * @param  [int] $product_id        [产品id]
     * @param  [int] $goods_id          [商品id]
     * @return [type]        [description]
     */
    /*public function relieveRelation($product_id, $goods_id){

        return ProductSku::where('product_id', $product_id)
                                    ->where('goods_id', $goods_id)
                                    ->delete();

    }//end func*/

    /**
     * 根据product_sku表的id更改产品关联的商品及属性组
     * @param  [int] $id              [产品sku表主键id]
     * @param  [int] $goods_id        [商品id]
     * @param  [varchar] $attribute_group [商品的属性以及对应的属性值的json字符串]
     * @return [type]                 [description]
     */
    /*public function changeRelation($id, $goods_id, $attribute_group){
        $product_sku = ProductSku::find($id);
        $product_sku->goods_id = $goods_id;
        $product_sku->attribute_group = $attribute_group;
        return $product_sku->save();

    }//end func*/


    /**
     * 根据产品id获取对应的商品
     * @param  [int] $product_id        [产品id]
     * @return mixed
     */
    /*public function getByProductId($product_id){

        return ProductSku::where('product_id', $product_id)->get();

    }//end func*/


}